<%@ page language="java" contentType="text/html; charset=UTF-8"  isErrorPage="true"
    pageEncoding="UTF-8"%>
<div class="layuimini-main">

    <div class="layui-form layuimini-form" lay-filter="studentedit">
        <input type="hidden" name="id" value="">
         <div class="layui-form-item">
            <label class="layui-form-label required">用户ID</label>
            <div class="layui-input-block">
                <input type="text" name="userId" lay-verify="required" lay-reqtext="用户ID不能为空" 
                placeholder="请输入用户ID" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" lay-reqtext="姓名不能为空" 
                placeholder="请输入姓名" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">班级</label>
            <div class="layui-input-block">
                <input type="text" name="classes" lay-verify="required" lay-reqtext="班级不能为空" 
                placeholder="请输入班级" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">年级</label>
            <div class="layui-input-block">
                <input type="text" name="grade" lay-verify="required" lay-reqtext="年级不能为空" 
                placeholder="请输入年级" value="" class="layui-input">
            </div>
        </div>
         <div class="layui-form-item">
            <label class="layui-form-label required">年龄</label>
            <div class="layui-input-block">
                <input type="text" name="age" lay-verify="required" lay-reqtext="年龄不能为空" 
                placeholder="请输入年龄" value="" class="layui-input">
            </div>
        </div>
         <div class="layui-form-item">
              <label class="layui-form-label required">性别</label>
              <div class="layui-input-block">
                  <select name="sex" lay-filter="role">
                      <option value="">请选择性别</option>
                      <option value="男">男</option>
                      <option value="女">女</option>
                  </select>
              </div>
         </div>
       

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script src="/PC_educationms/js/jquery-3.3.1.js"></script>
<script>
    layui.use(['form', 'table', 'upload', 'element', 'laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            upload = layui.upload,
            element = layui.element,
            laydate = layui.laydate,
            $ = layui.$;
        
        laydate.render({
        	elem:'#date',
        	type:'datetime',
        	trigger:'click'
        })

        $('#demo1').attr('src', parent.image);
        //常规使用 - 普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: 'uploadServlet' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            ,auto: true
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });

                element.progress('demo', '0%'); //进度条复位
                layer.msg('上传中', {icon: 16, time: 0});
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功的一些操作
                console.log(55)
                console.log(res)
                $("#image").val(res.data.src)
                //……
                $('#demoText').html(''); //置空上传失败的状态
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
            //进度条
            ,progress: function(n, elem, e){
                element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
                if(n == 100){
                    layer.msg('上传完毕', {icon: 1});
                }
            }
        });
        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert("确定修改学生信息！", {
                title: '提交信息'
            }, function () {
                 console.log(data);
                 $.ajax({
                	 url:"/PC_educationms/student/updateStudent",
                	 data:JSON.stringify(data.field),
                	 type:"POST",
                	 dataType:"json",
                     contentType:"application/json",
                	 success:function(data){
                		 console.log(data);
                   		 // data = JSON.parse(data);
                   		 if(data.msg == "ok") {
                   			// 关闭弹出层
                            layer.close(index);
                            layer.close(parentIndex);
                            $(".layui-laypage-btn")[0].click();
                   		 }else{
                             layer.alert('修改失败！请输入正确信息。'+(data.handle!=undefined?data.handle:""))
                     	}
                	 }
                 })
            });
            return false;
        });

    });
</script>